package com.nowcoder.code.lcof;

import com.algorithm.model.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;

public class JZ32 {
    public static void main(String[] args) {
        System.out.println(new JZ32().PrintFromTopToBottom(new TreeNode(0)));
    }

    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        if (root == null) {
            return null;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        LinkedList<TreeNode> tmp = new LinkedList<>();
        tmp.add(root);
        int i = 1;
        while (i > 0) {
            TreeNode treeNode = tmp.poll();
            arrayList.add(treeNode.val);
            i--;
            if (treeNode.left != null) {
                tmp.add(treeNode.left);
                i++;
            }
            if (treeNode.right != null) {
                tmp.add(treeNode.right);
                i++;
            }
        }
        return arrayList;
    }

}
